﻿using IDal;
using Model.DTO;
using Model.House;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Formats.Asn1.AsnWriter;

namespace Dal
{
    public class HouseDal:IHouseDal
    {
        private readonly HotelDbContext db;

        //构造函数方式注入
        public HouseDal(HotelDbContext db)
        {
            this.db = db;
        }
        /// <summary>
        /// 获取酒店类型表数据
        /// </summary>
        /// <returns>获取酒店类型表数据</returns>
        public IQueryable<HotelType> ShowHotelType()
        {
            return db.HotelTypes;
        }
       
        ///显示酒店信息+通过酒店类型查询+分页
        /// </summary>
        /// <param name="typeid">酒店类型</param>
        /// <param name="pageindex">当前页</param>
        /// <param name="pagesize">每页显示的条数</param>
        /// <returns>通过条件查询和分页显示的当前数据</returns>
        public dynamic ShowHotel(int? typeid = -1, int pageindex = 1, int pagesize = 3)
        {
            IQueryable<HotelDto> list = from a in db.Hotel
                                        join b in db.HotelTypes
                                        on a.HotelTypeId equals b.HotelTypeId
                                        select new HotelDto()
                                        {
                                            HotelId = a.HotelId,
                                            HotelName = a.HotelName,
                                            HotelTypeId = a.HotelTypeId,
                                            Score = a.Score,
                                            HotelMessage = a.HotelMessage,
                                            Left = a.Left,
                                            Right = a.Right,
                                            HotelTypeName = b.HotelTypeName
                                        };
            if (typeid != -1)
            {
                list = list.Where(x => x.HotelTypeId.Equals(typeid));
            }
            return new
            {
                //获取到总条数
                listcount=list.Count(),
                //Skip跳过 Take是连续
                rows = list.OrderBy(x=>x.HotelId).Skip((pageindex-1)*pagesize).Take(pagesize)
            };
        }
        /// <summary>
        /// 百度地图
        /// </summary>
        /// <param name="id">通过主键获取到整体数据</param>
        /// <returns>获取经度纬度</returns>
        public Hotel FindHotel(int id)
        {
            return db.Hotel.Find(id);
        }
        /// <summary>
         /// 通过选酒店的房间+显示+查询+分页
         /// </summary>
         /// <param name="hotelid">酒店id不可为空</param>
         /// <param name="min">面积最小值</param>
         /// <param name="max">面积最大值</param>
         /// <param name="typeid">房型id</param>
         /// <param name="pageindex">当前页</param>
         /// <param name="pagesize">每页显示的条数</param>
         /// <returns>通过条件查询和分页显示当前页面的数据</returns>
        public dynamic ShowHouse(int hotelid, decimal? min, decimal? max, int? typeid = -1, int pageindex = 1, int pagesize = 3)
        {
            IQueryable<HouseDto> list = from a in db.Houses
                                        join b in db.HouseTypes
                                        on a.HouseTypeId equals b.HouseTypeId
                                        join c in db.Hotel
                                        on a.HotelId equals c.HotelId
                                        select new HouseDto()
                                        {
                                            HouseId = a.HouseId,
                                            HouseName = a.HouseName,
                                            HotelId = a.HotelId,
                                            HouseTypeId = a.HouseTypeId,
                                            HouseImg = a.HouseImg,
                                            HaveWindow = a.HaveWindow,
                                            HaveWifi = a.HaveWifi,
                                            Arec = a.Arec,
                                            People = a.People,
                                            HouseTypeName = b.HouseTypeName,
                                            HotelName = c.HotelName,
                                            HousePrice=a.HousePrice
                                        };
            list = list.Where(x => x.HotelId.Equals(hotelid));
            if (min != null)
            {
                list = list.Where(x => x.Arec >= min);
            }
            if (max != null)
            {
                list = list.Where(x => x.Arec <= max);
            }
            if (typeid != -1)
            {
                list = list.Where(x => x.HouseTypeId.Equals(typeid));
            }
            return new
            {
                listcount = list.Count(),
                rows = list.OrderBy(x => x.HouseId).Skip((pageindex - 1) * pagesize).Take(pagesize)
            };

        }
        /// <summary>
        /// 获取房型表信息
        /// </summary>
        /// <returns>获取房型表信息</returns>
        public IQueryable<HouseType> ShowHouseType()
        {
            return db.HouseTypes;
        }
        /// <summary>
        /// 房间
        /// </summary>
        /// <param name="house">房间表</param>
        /// <returns>返回受影响行数</returns>
        public int AddHouse(House house)
        {
            db.Houses.Add(house);
            return db.SaveChanges();
        }
        /// <summary>
        /// 获取所有酒店信息
        /// </summary>
        /// <returns>获取所有酒店信息</returns>
        public IQueryable<Hotel> ShowHotels()
        { 
            return db.Hotel;
        }
        /// <summary>
        /// 通过酒店获取房间信息
        /// </summary>
        /// <param name="hotelid">酒店的id</param>
        /// <returns>通过酒店获取房间信息</returns>
        public IQueryable<House> ShowHouseGetImg(int hotelid)
        {
            return db.Houses.Where(x => x.HotelId.Equals(hotelid));
        }
        /// <summary>
        /// 获取房间的具体信息
        /// </summary>
        /// <param name="houseid">房间if</param>
        /// <returns>通过主键返回房间的具体信息</returns>
        public HouseDto Bindhouse(int houseid)
        {
            IQueryable<HouseDto> list = from a in db.Houses
                                        join b in db.HouseTypes
                                        on a.HouseTypeId equals b.HouseTypeId
                                        join c in db.Hotel
                                        on a.HotelId equals c.HotelId
                                        select new HouseDto()
                                        {
                                            HouseId = a.HouseId,
                                            HouseName = a.HouseName,
                                            HotelId = a.HotelId,
                                            HouseTypeId = a.HouseTypeId,
                                            HouseImg = a.HouseImg,
                                            HaveWindow = a.HaveWindow,
                                            HaveWifi = a.HaveWifi,
                                            Arec = a.Arec,
                                            People = a.People,
                                            HouseTypeName = b.HouseTypeName,
                                            HotelName = c.HotelName,
                                            HousePrice = a.HousePrice
                                        };
            //FirstOrDefault  First第一条 or或者 Default默认 
            return list.Where(x => x.HouseId.Equals(houseid)).FirstOrDefault();
        }
        /// <summary>
        /// 房间显示
        /// </summary>
        /// <param name="housename">房间名称进行模糊查询</param>
        /// <param name="min">价格最小值</param>
        /// <param name="max">价格最大值</param>
        /// <param name="pageindex">当前页</param>
        /// <param name="pagesize">每页现实的条数</param>
        /// <returns>通过条件查询个分页显示当前页面展示的数据</returns>
        public dynamic ShowHouseDto(string? housename = "", decimal? min = null, decimal? max = null, int pageindex = 1, int pagesize = 3)
        {
            IQueryable<HouseShowDto> list = from a in db.Houses
                                            join b in db.HouseTypes
                                            on a.HouseTypeId equals b.HouseTypeId
                                            select new HouseShowDto()
                                            {
                                                HouseId = a.HouseId,
                                                HouseName = a.HouseName,
                                                HotelId = a.HotelId,
                                                HouseTypeId = a.HouseTypeId,
                                                HouseImg = a.HouseImg,
                                                HaveWindow = a.HaveWindow,
                                                HaveWifi = a.HaveWifi,
                                                Arec = a.Arec,
                                                People = a.People,
                                                HousePrice = a.HousePrice,
                                                HouseTypeName = b.HouseTypeName
                                            };
            if (!string.IsNullOrEmpty(housename))
            {
                list = list.Where(x => x.HouseName.Contains(housename));
            }
            if (min != null)
            {
                list = list.Where(x => x.HousePrice >= min);
            }
            if (max != null)
            {
                list = list.Where(x => x.HousePrice <= max);
            }
            return new
            {
                //总条数
                listcount=list.Count(),
                //每页显示的数据
                rows=list.OrderBy(x=>x.HouseId).Skip((pageindex-1)*pagesize).Take(pagesize)
            };
        }
        /// <summary>
        /// 通过房间id做的反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public HouseShowDto GetHouseId(int id)
        {
            IQueryable<HouseShowDto> list = from a in db.Houses
                                            join b in db.HouseTypes
                                            on a.HouseTypeId equals b.HouseTypeId
                                            select new HouseShowDto()
                                            {
                                                HouseId = a.HouseId,
                                                HouseName = a.HouseName,
                                                HotelId = a.HotelId,
                                                HouseTypeId = a.HouseTypeId,
                                                HouseImg = a.HouseImg,
                                                HaveWindow = a.HaveWindow,
                                                HaveWifi = a.HaveWifi,
                                                Arec = a.Arec,
                                                People = a.People,
                                                HousePrice = a.HousePrice,
                                                HouseTypeName = b.HouseTypeName
                                            };
            return list.Where(x => x.HouseId.Equals(id)).FirstOrDefault();

        }
    }
}
